package xyz.labmem.lipx.core.util

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.io.file.FileReader
import cn.hutool.core.io.file.FileWriter
import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import xyz.labmem.lipx.core.AppContext
import xyz.labmem.lipx.core.Status
import xyz.labmem.lipx.core.entity.ClientConfig
import xyz.labmem.lipx.core.tool.appPath
import java.io.File
import java.util.*
import kotlin.collections.HashSet


/**
 * @description: 持久化配置信息
 * @author: ltty
 * @date: 2022/6/15 12:23
 */

class ClientConfigDataUtil {

    companion object {

        private val cap = "$appPath/lipxClient"

        fun init() {
            val dir = File(cap)
            if (!dir.exists()) {
                dir.mkdirs()
            }
            refreshList()
        }


        private fun read(): JSONObject? {
            File("$cap/lipxData.json").apply {
                if (exists()) {
                    val data = FileReader(this).readString()
                    try {
                        return JSON.parseObject(data)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                } else {
                    write(JSONObject())
                    return read()
                }
            }
            return null
        }

        //写入文件
        fun write(info: ClientConfig) {
            val config = ClientConfig()
            BeanUtil.copyProperties(info, config)
            config.status = Status.IDLE
            config.version = ""
            var data = read()
            if (data != null) {
                data[config.id] = config
            } else {
                data = JSONObject().apply {
                    set(config.id, config)
                }
            }
            write(data)
        }

        fun refreshList() {
            AppContext.Client.groupList.clear()
            val cache = HashMap<String, ClientConfig>()
            val group = HashSet<String>()
            read()?.let {
                it.keys.forEach { k ->
                    it.getJSONObject(k).toJavaObject(ClientConfig::class.java).apply {
                        cache[id!!] = this
                        group.add(this.group)
                    }
                }
            }
            cache.forEach { (k, v) ->
                if (AppContext.Client.allList.containsKey(k)) {
                    v.status = AppContext.Client.allList[k]!!.status
                    v.version = AppContext.Client.allList[k]!!.version
                }
            }
            AppContext.Client.allList.clear()
            AppContext.Client.allList.putAll(cache)
            AppContext.Client.groupList = LinkedList(group.sorted())
        }

        private fun write(json: JSONObject) {
            FileWriter("$cap/lipxData.json").write(json.toJSONString())
        }

        fun delById(id: String) {
            read()?.let {
                it.remove(id)
                //写入文件
                write(it)
            }
        }
    }

}